<?php
class AddMenuItem extends Action {
	/*
	 * (non-PHPdoc) @see Action::execute()
	 */
	public function execute($params) {
		check_login ( ADMIN_LOGIN_PAGE );
		$type = rqst ( 'type', 'url' );
		$target = rqst ( 'target', '_blank' );
		$menu_name = trim ( rqst ( 'menu_name' ) );
		if (empty ( $menu_name )) {
			echo "error:未指定菜单";
			Response::close ( true );
		}
		$items = array ();
		if ($type == 'url') {
			$items = $this->addUrlItem ( $menu_name, $target );
		} else if ($type == 'cate') { // 添加分类
			$items = $this->addCateItem ( $menu_name, $target );
			if (empty ( $items )) {
				echo "error:频道栏目必须发布成页面后才能添加到菜单。";
				Response::close ( true );
			}
		} else if ($type == 'page') {
			$items = $this->addPageItem ( $menu_name, $target );
		}
		$data ['items'] = $items;
		$data ['adding'] = true;
		return new SmartyView ( $data, 'vfs/AddMenuItem' );
	}
	// 添加自定义菜单项
	private function addUrlItem($menu_name, $target) {
		$items = array ();
		$item = rqsts ( array ('item_name','title','url' ) );
		$item ['type'] = 'url';
		$item ['target'] = $target;
		$item ['menu_name'] = $menu_name;
		$item ['up_id'] = 0;
		$item ['sort'] = 999;
		$miM = new WebMenuItemModel ();
		$valid = $this->validate ( 'menuitem', $item );
		if ($valid === true) {
			$rst = $miM->insert ( $item );
			if ($rst != false) {
				$item = $miM->read ( $rst ['menuitem_id'] );
				$item ['type_name'] = '自定义';
				$items [] = $item;
			}
		} else {
			echo 'error:' . implode ( "<br/>", $valid );
			Response::close ( true );
		}
		return $items;
	}
	// 添加页面菜单项
	private function addPageItem($menu_name, $target) {
		$items = array ();
		$ids = safe_ids ( rqst ( 'ids' ), ',', true );
		
		if (empty ( $ids )) {
			return $items;
		}
		$item ['menu_name'] = $menu_name;
		$item ['target'] = $target;
		$item ['type'] = 'page';
		$item ['sort'] = 999;
		$item ['up_id'] = 0;
		$pM = new WebPageModel ();
		$miM = new WebMenuItemModel ();
		foreach ( $ids as $pid ) {
			if (empty ( $pid )) {
				continue;
			}
			$page = $pM->read ( $pid );
			if (! $page) {
				continue;
			}
			$item ['page_id'] = $pid;
			$item ['item_name'] = empty ( $page ['title'] ) ? '未命名' : $page ['title'];
			$item ['title'] = $page ['subtitle'];
			$itm = $miM->insert ( $item );
			if ($item) {
				$itm ['type_name'] = '页面';
				$items [] = $itm;
			}
		}
		return $items;
	}
	private function addCateItem($menu_name, $target) {
		$items = array ();
		$ids = safe_ids ( rqst ( 'ids' ), ',', true );
		
		if (empty ( $ids )) {
			return $items;
		}
		$item ['menu_name'] = $menu_name;
		$item ['target'] = $target;
		$item ['type'] = 'cate';
		$item ['sort'] = 999;
		$item ['up_id'] = 0;
		$cM = new WebPageCategory ();
		$miM = new WebMenuItemModel ();
		foreach ( $ids as $cid ) {
			if (empty ( $cid )) {
				continue;
			}
			$cate = $cM->read ( $cid );
			if (! $cate || empty ( $cate ['page_id'] )) {
				continue;
			}
			$item ['page_id'] = $cate ['page_id'];
			$item ['item_name'] = $cate ['title'];
			$item ['title'] = '';
			$itm = $miM->insert ( $item );
			if ($item) {
				$itm ['type_name'] = '频道栏目';
				$items [] = $itm;
			}
		}
		return $items;
	}
}